package com.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.FileInputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class JDBCUtil {
//    private static String driver;
//    private static String url;
//    private static String username;
//    private static String password;
    private static DruidDataSource druid;

    static {
        try {
            Properties pro = new Properties();
            pro.load(new FileInputStream("src/druid.properties"));
//            driver = pro.getProperty("driver");
//            url = pro.getProperty("url");
//            username = pro.getProperty("username");
//            password = pro.getProperty("password");
              druid= (DruidDataSource) DruidDataSourceFactory.createDataSource(pro);
//            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取连接方法
    public static Connection getConnection() {
        try {
//            Connection con = DriverManager.getConnection(url, username, password);
            return druid.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //关闭方法
    public static void Close(Statement st) throws SQLException {
        try {
            if (st != null) {
                st.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    public static void Close(Connection co) throws SQLException {
        try {
            if (co != null) {
                co.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    public static void Close(ResultSet resultSet) throws SQLException {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    public static void Close(Statement st, Connection co) throws SQLException {
        Close(st);
        Close(co);
    }
    //关闭查询方法
    public static void Close(ResultSet resultSet, Statement st, Connection co) throws SQLException {
        Close(st);
        Close(co);
        Close(resultSet);
    }

    //dml操作----不考虑事务
    public static int dml(String sql, Object... args) {
        Connection connection = getConnection();
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            if (args != null) {
                for (int i = 0; i < args.length; i++) {
                    ps.setObject(i + 1, args[i]);
                }
            }
            int i = ps.executeUpdate();
            return i;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    //dml操作----考虑事务
    public static void dml(Connection co,String sql,Object...args) throws SQLException {
        co.setAutoCommit(false);
        PreparedStatement pr = co.prepareStatement(sql);
        if (args!=null){
            for (int i = 0; i < args.length; i++) {
                pr.setObject(i+1,args[i]);
            }
        }
        pr.executeUpdate();
        Close(pr);
    }



    private Connection con;
    public JDBCUtil(){
        con=getConnection();
        try {
            con.setAutoCommit(false);
        }catch (Exception e){
            e.printStackTrace();
            con=null;
        }
    }
    public JDBCUtil sql_center(String sql,Object...args) throws SQLException {
        if (con!=null){
            PreparedStatement pr = null;
            try {
                pr=con.prepareStatement(sql);
                if (args!=null){
                    for (int i = 0; i < args.length; i++) {
                        pr.setObject(i+1,args[i]);
                    }
                }
                pr.executeUpdate();
            }catch (Exception e){
                e.printStackTrace();
                con=null;
            }finally {
                Close(pr);
            }
        }
        return this;
    }
    public void sql_end() throws SQLException {
        try {
            if (con!=null){
                con.commit();
            }else {
                con.rollback();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            Close(con);
        }
    }

    //dql
    public static <E> ArrayList<E> dql(String sql, Class<E> c, Object...args){
        ArrayList<E> list=new ArrayList<>();
        Connection con = getConnection();
        PreparedStatement p=null;
        ResultSet rs=null;
        try {
            p=con.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                p.setObject(i+1,args[i]);
            }
            rs=p.executeQuery();
            while (rs.next()){
                E e = c.newInstance();
                Field[] declaredFields = c.getDeclaredFields();
                for (Field field : declaredFields) {
                    String name = field.getName();
                    try {
                        Object value = rs.getObject(name);
                        //为指定的属性进行赋值
                        field.setAccessible(true);
                        field.set(e,value);
                    }catch (Exception e1){

                    }
                }
                list.add(e);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return list;
    }
}
